package com.tay.MapReduce;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

public class WordCountMain {

	public static void main(String[] args) throws Throwable {

		// 这里如果不设置conf的话，它会读取本地默认的conf配置
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);

		// 指定main方法所在的类
		job.setJarByClass(WordCountMain.class);
		// job.setJar("/root/wordcount2.jar");

		// 指定map类和reduce类
		job.setMapperClass(WordCountMap.class);
		job.setReducerClass(WordCountReduce.class);

		// 指定map的输出key和value的数据类型
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(IntWritable.class);

		// 指定reduce的输出key和value的类型，NullWritable相当于一个占位符，key或者value不使用时可以用NullWritable占位
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);

		FileInputFormat.setInputPaths(job, new Path("/hadoop_test/input"));
		FileOutputFormat.setOutputPath(job, new Path("/hadoop_test/output"));

		// 不启用reduce阶段
		// job.setNumReduceTasks(0);

		boolean res = job.waitForCompletion(true);
		System.exit(res ? 0 : 1);

	}

}
